package com.fx.ruankao.service.impl;

import com.fx.ruankao.entity.ExamPaper;
import com.fx.ruankao.mapper.ExamPaperMapper;
import com.fx.ruankao.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/**
 * 试卷服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ExamPaperServiceImpl implements ExamPaperService {
    
    @Autowired
    private ExamPaperMapper examPaperMapper;
    
    @Override
    public int insert(ExamPaper examPaper) {
        // 设置创建时间
        examPaper.setCreateTime(new java.util.Date());
        // 默认状态为未发布
        if (examPaper.getStatus() == null) {
            examPaper.setStatus(0);
        }
        return examPaperMapper.insert(examPaper);
    }
    
    @Override
    public int deleteById(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("试卷ID不能为空");
        }
        return examPaperMapper.deleteById(id != null ? id.longValue() : null);
    }
    
    @Override
    public int update(ExamPaper examPaper) {
        // 更新时间
        examPaper.setUpdateTime(new java.util.Date());
        return examPaperMapper.update(examPaper);
    }
    
    @Override
    public ExamPaper selectById(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("试卷ID不能为空");
        }
        return examPaperMapper.selectById(id != null ? id.longValue() : null);
    }
    
    @Override
    public List<ExamPaper> selectAll() {
        return examPaperMapper.selectList(new ExamPaper());
    }
    
    @Override
    public List<ExamPaper> selectByTitle(String title) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setTitle(title);
        return examPaperMapper.selectList(examPaper);
    }
    
    @Override
    public List<ExamPaper> selectBySubject(String subject) {
        // 注意：数据库表exam_paper中没有subject字段，此方法暂时返回空列表
        // 实际项目中应该根据需求重新设计此功能
        return new java.util.ArrayList<>();
    }
    
    @Override
    public List<ExamPaper> selectByStatus(Integer status) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setStatus(status);
        return examPaperMapper.selectList(examPaper);
    }
    
    @Override
    public int batchDelete(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            throw new IllegalArgumentException("请至少选择一个要删除的试卷");
        }
        // 实际项目中应该在ExamPaperMapper中添加batchDelete方法
        int count = 0;
        for (Integer id : ids) {
            if (id != null) {
                count += examPaperMapper.deleteById(id.longValue());
            }
        }
        return count;
    }
    
    @Override
    public boolean updateStatus(Integer id, Integer status) {
        if (id == null) {
            throw new IllegalArgumentException("试卷ID不能为空");
        }
        if (status == null || (status != 0 && status != 1 && status != 2)) {
            throw new IllegalArgumentException("状态值无效，只能为0(未发布)、1(已发布)、2(已结束)");
        }
        
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(id.longValue());
        examPaper.setStatus(status);
        examPaper.setUpdateTime(new Date());
        // 注意：数据库表exam_paper中没有publishTime字段，已移除相关设置
        
        return examPaperMapper.updateStatus(id.longValue(), status) > 0;
    }
    
    @Override
    public int countPapers() {
        return examPaperMapper.selectCount(new ExamPaper());
    }
    
    @Override
    public int countPapersByCondition(String subject, Integer status) {
        // 注意：数据库表exam_paper中没有subject字段，此方法仅按status统计
        ExamPaper examPaper = new ExamPaper();
        examPaper.setStatus(status);
        return examPaperMapper.selectCount(examPaper);
    }

    @Override
    public List<ExamPaper> selectByCreator(Integer userId) {
        if (userId == null) {
            throw new IllegalArgumentException("创建者ID不能为空");
        }
        ExamPaper examPaper = new ExamPaper();
        examPaper.setCreateBy(userId.longValue());
        return examPaperMapper.selectList(examPaper);
    }

    @Override
    public List<ExamPaper> selectByRoleId(Integer roleId) {
        if (roleId == null) {
            throw new IllegalArgumentException("角色ID不能为空");
        }
        // 注意：ExamPaperMapper中没有selectByRoleId方法，这里返回空列表
        // 实际项目中应该根据需求实现按角色ID查询试卷的逻辑
        return new java.util.ArrayList<>();
    }

    @Override
    public int countBySubject(String subject) {
        // 注意：数据库表exam_paper中没有subject字段，此方法暂时返回0
        // 实际项目中应该根据需求重新设计此功能
        return 0;
    }
}